import { defHttp } from '@/utils/http/axios';

import {
  StaticInfo,
  StaticParams,
  StaticListGetResultModel,
  StaticAddReq,
  StaticEditReq,
  StaticDelReq,
  StaticUploadResp,
} from './model/staticModel';

enum Api {
  List = '/api/admin/v1/static/list',
  Create = '/api/admin/v1/static/add',
  Update = '/api/admin/v1/static/edit',
  Delete = '/api/admin/v1/static/del',
  Info = '/api/admin/v1/static/info',
  Upload = '/api/admin/v1/static/upload',
}

/**
 * @description: 获取静态资源列表
 */
export const getStaticList = (params: StaticParams) =>
  defHttp.get<StaticListGetResultModel>({
    url: Api.List,
    params,
    headers: { 'Content-Type': 'multipart/form-data' },
  });

/**
 * @description: 创建静态资源
 */
export const createStatic = (params: StaticAddReq) =>
  defHttp.post<{ id: number }>({ url: Api.Create, params });

/**
 * @description: 更新静态资源
 */
export const updateStatic = (params: StaticEditReq) =>
  defHttp.put<void>({ url: Api.Update, params });

/**
 * @description: 删除静态资源
 */
export const deleteStatic = (params: StaticDelReq) =>
  defHttp.delete<void>({ url: Api.Delete, params });

/**
 * @description: 获取静态资源详情
 */
export const getStaticInfo = (id: number) =>
  defHttp.get<StaticInfo>({ url: Api.Info, params: { id } });

/**
 * @description: 上传静态资源
 */
export const uploadStatic = async (file: File): Promise<StaticUploadResp> => {
  const formData = new FormData();
  formData.append('file', file);
  const res = await fetch(Api.Upload, {
    method: 'POST',
    body: formData,
  });
  return res.json();
};
